<?php

/**
 * Msa_ExtraClientFieldObject - Gestion des champs supplémentaires pour les clients
 *
 * @package
 * @author Alexis Eric
 * @copyright Copyright (c) 2012 - Erra Prod
 * @version $1.0$
 * @access public
 */
class Msa_ExtraClientFieldObject extends Erra_Fields_Base_Object {

    function __construct($objectId, $buildArgs = null) {
        parent::__construct($objectId, $buildArgs);

        $type = array('Erra_Fields_Text' => 'Champ texte',
            'Erra_Fields_TextArea' => 'Champ multiligne',
            'Erra_Fields_ComboBox' => 'Boite de selection multiple',
            'Erra_Fields_Date' => 'Champ date',
            'Erra_Fields_Radio' => 'Bouton radio');

        $activate = Erra_Fields_ComboBox::create('activate')->fill(array('inactive' => 'Inactif', 'active' => 'Actif'))
                ->setDescription('Statut')
                ->setInAdminList(true);

        $field_id = Erra_Fields_Hidden::create('extra_field_id')->fill($type);
        $field_type = Erra_Fields_ComboBox::create('extra_field_type')->fill($type);
        $field_name = Erra_Fields_Text::create('extra_field_name')
                ->setDescription('Libellé du champ')
                ->setInAdminList(true);
        $field_desc = Erra_Fields_Text::create('extra_field_description')
                ->setDescription('Description du champ');

        $field_data = Erra_Fields_TextArea::create('extra_field_data')
                ->setRows(2)
                ->setDescription('Données du champ (Optionnel)');

        $this->addFields($activate, $field_id, $field_type, $field_name, $field_desc, $field_data);
        $this->setCRUD('RUS');
        $this->setAttachedActionMethod('SaveAction/saveExtraFieldsDefinition');
        $this->setDescription('Données supplémentaires');
        $this->setAddText('Cliquez ici pour ajouter une donnée client supplémentaire');
    }

    public function postInit() {
        if ($this->extra_field_type->value == 'Erra_Fields_ComboBox' || $this->extra_field_type->value == 'Erra_Fields_Radio') {
            $this->extra_field_data->setAttribute('style', '');
        }
        $this->extra_field_data->prepend('<div class="extra-details">' . self::echoExtraDetails($this->extra_field_type->value) . '</div>');
    }

    /**
     * Remplace les champs hidden inserés par la fonction Msa_ClientObject::addExtraFieldsIds par leur vrai contenu, si les champs sont activés 
     * 
     * @param Erra_Fields_Base_Object $userObject
     */
    public static function addExtraFields(Erra_Fields_Base_Object $userObject) {
        $fieldInfos = Erra_Core_DatabasePdo::SELECT('SELECT * FROM msa_extraclientfieldobject ORDER BY id DESC');
        if (Erra_Core_DatabasePdo::rowCount()) {
            while ($fieldInfo = $fieldInfos->fetch()) {
                if ($fieldInfo->activate == 'active') {
                    $fieldType = $fieldInfo->extra_field_type;
                    $field = $fieldType::create($fieldInfo->extra_field_id)
                            ->setDescription($fieldInfo->extra_field_name);
                    if ($fieldType == 'Erra_Fields_ComboBox' || $fieldType == 'Erra_Fields_MultipleCheckBox' || $fieldType == 'Erra_Fields_Radio') {
                        $field_data = $fieldInfo->extra_field_data;
                        if (strlen($field_data)) {
                            $dataArray = explode(',', $field_data);
                            $keysArray = array();
                            foreach ($dataArray as $item) {
                                $keysArray[] = strtolower(strip_non_alnum($item));
                            }
                            $field->fill(array_combine($keysArray, $dataArray));
                        }
                    }
                    $userObject->addField($field);
                }
            }
        }
    }

    /**
     * Affichage des informations d'édition pour les champs
     * 
     * @param type $fieldType
     * @return string
     */
    public static function echoExtraDetails($fieldType) {
        $htmlContent = array();
        $htmlContent["Erra_Fields_Text"] = '<div class="Erra_Fields_Text"></div>';
        $htmlContent["Erra_Fields_TextArea"] = '<div class="Erra_Fields_TextArea"></div>';
        $htmlContent["Erra_Fields_ComboBox"] = '<div class="Erra_Fields_ComboBox">
            <i>Vous pouvez entrer les différentes valeurs de la boite de selection<br/> separées par des virgules de la manière suivante <br/></i>
        <strong>Option 1, Option 2, Option 3</strong><br/>
        <font color="red">Exemple ci-dessous</font><br/>
        <img src="' . config_get('public_images') . '/help/combobox_demo.jpg" class="img-rounded"/>
        </div>';
        $htmlContent["Erra_Fields_Date"] = '<div class="Erra_Fields_Date"></div>';
        $htmlContent["Erra_Fields_Radio"] = '<div class="Erra_Fields_Radio">
            <i>Vous pouvez entrer les différentes options<br/> separées par des virgules de la manière suivante <br/></i>
        <strong>Option 1, Option 2, Option 3</strong><br/>
        <font color="red">Exemple ci-dessous</font><br/>
        <img src="' . config_get('public_images') . '/help/radio_demo.jpg" class="img-rounded"/>
        </div>';
        return $htmlContent[$fieldType];
    }

    public static function echoAllExtraDetails() {
        $res = '<div class="extra-detail-content" style="display:none;">';
        $res .= self::echoExtraDetails('Erra_Fields_Text');
        $res .= self::echoExtraDetails('Erra_Fields_TextArea');
        $res .= self::echoExtraDetails('Erra_Fields_ComboBox');
        $res .= self::echoExtraDetails('Erra_Fields_Date');
        $res .= self::echoExtraDetails('Erra_Fields_Radio');
        $res .= '</div>';
        return $res;
    }

}